//
//  EqpSetViewController.m
//  Hive
//
//  Created by 惠俊贝 on 15/9/3.
//  Copyright (c) 2015年 ZM. All rights reserved.
//

#import "EqpSetViewController.h"
#import "ABNetworkUtil.h"
#import "EqpInfo.h"
#import "AreaScopeInfo.h"

@interface EqpSetViewController () <UITableViewDataSource, UITableViewDelegate> {
    NSArray *cellNames;
    NSArray *areaScopeInfoArr;
    
    __weak IBOutlet UITableView *areaScopeTableView;
    
    __weak IBOutlet UISwitch *OnOfOffSwitch;
}

@end

@implementation EqpSetViewController
@synthesize eqpInfo;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    cellNames = @[
                  @"设置围栏"
//                  ,@"短信提醒"
                  ];
    
    backButton.hidden = NO;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self loadData];
    
    NSString *eqpName = eqpInfo.EqpName == nil ? @"":eqpInfo.EqpName;
    titleLabel.text = eqpName;
    
}

- (void)loadData {
    NSString *deviceId = self.eqpInfo.DeviceID;
    [[[ABNetworkUtil alloc] init]
     queryAreaScopeWithDeviceID:deviceId
     success:^(id response) {
         NSLog(@"%@",response);
         NSDictionary *dic = (NSDictionary *)response;
         if ([[dic objectForKey:@"Result"] integerValue] == 0) {
             //成功
             NSArray *arr = [[dic objectForKey:@"DataList"]
                             objectForKey:@"BIZ_AreaScopeInfo"];
             areaScopeInfoArr = [AreaScopeInfo createAreaScopeInfosWithDicArray:arr];
             [areaScopeTableView reloadData];
         }
     }
     fail:^(NSError *error) {
         
     }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - events
- (IBAction)addAreaScopeButtonPress:(id)sender {
    
}

- (IBAction)onOrOffSwitch:(id)sender {
    areaScopeTableView.hidden = !OnOfOffSwitch.on;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return areaScopeInfoArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
        cell.contentView.backgroundColor = [UIColor colorWithRed:0.921 green:0.921 blue:0.921 alpha:1.0];
        AreaScopeInfo *asi = [areaScopeInfoArr objectAtIndex:indexPath.row];
        cell.textLabel.text = asi.AreaName==nil ? @"未命名":asi.AreaName;
        cell.detailTextLabel.text = asi.State ? @"围栏开启":@"围栏关闭";

    }
    return cell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.selected = NO;
    
    
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
